package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Spot;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.query.SpotQuery;
import cn.wolfcode.luowowo.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.repository.SpotRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ISpotService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

// 景点
@Service
//@Transactional mongodb事务必须复制集方式
public class SpotServiceImpl implements ISpotService {

    @Autowired
    private SpotRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Spot spot) {

        spot.setId(null);
        repository.save(spot);
    }

    @Override
    public void delete(String id) {

        repository.deleteById(id);
    }

    @Override
    public void update(Spot spot) {

        repository.save(spot);
    }

    @Override
    public Spot get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Spot> listAll() {
        return repository.findAll();
    }

    //景点分页查询(高级查询)
    @Override
    public Page<Spot> query(SpotQuery qo) {

        Query query = new Query();

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,
                qo.getPageSize(), Sort.Direction.ASC,"_id");

        return DBHelper.query(template,Spot.class,query,pageable);
    }

    // 添加按钮:添加/更新
    @Override
    public void saveOrUpdate(Spot spot) {

        if(StringUtils.hasLength(spot.getId())){

            this.update(spot);//更新
        }else{
            this.save(spot);//保存
        }
    }


}
